package com.IntelligenceInsight.Myth.service;

import com.IntelligenceInsight.Myth.mapper.UserMapper;
import com.IntelligenceInsight.Myth.model.User;
import com.IntelligenceInsight.Myth.util.NoteRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    @Autowired
    private UserMapper mapper;

    // user login
    public NoteRequest Login(String username, String password) {
        NoteRequest res = new NoteRequest();
        User user = mapper.Login(username, password);
        if(user == null) {
            res.setStatus(1);
            res.setMsg("failure");
        } else {
            res.setStatus(0);
            res.setMsg("success");
            res.setData(user);
        }
        return res;
    }

    public NoteRequest UserNotExist(String username) {
        NoteRequest res = new NoteRequest();
        int n = mapper.UserCountByUserName(username);
        if(n > 0) {
            res.setStatus(1);
        } else {
            res.setStatus(0);
            res.setMsg("success");
        }
        return res;
    }

    public NoteRequest UserListAll() {
        NoteRequest res = new NoteRequest();
        List<User> user = mapper.UserListAll();
        if(user == null) {
            res.setStatus(1);
        } else {
            res.setStatus(0);
            res.setMsg("success");
            res.setData(user);
        }
        return res;
    }

    public NoteRequest UserListByUserType(int userType) {
        NoteRequest res = new NoteRequest();
        List<User> user = mapper.UserListByUserType(userType);
        if(user == null) {
            res.setStatus(1);
        } else {
            res.setStatus(0);
            res.setMsg("success");
            res.setData(user);
        }
        return res;
    }

    public NoteRequest UserAdd(String username, String password, int userType, String email, String realName){
        NoteRequest res = new NoteRequest();
        int n = mapper.UserAdd(username, password, userType, email, realName);
        if(n < 1) {
            res.setStatus(1);
        } else {
            res.setStatus(0);
            res.setMsg("success");
        }
        return res;
    }

    public NoteRequest UserPasswordChange(String password, String username) {
        NoteRequest res = new NoteRequest();
        int n = mapper.UserPasswordChange(password, username);
        if(n < 1) {
            res.setStatus(1);
        } else {
            res.setStatus(0);
            res.setMsg("success");
        }
        return res;
    }

    public NoteRequest UserInfoChange(String email, String realName, String username) {
        NoteRequest res = new NoteRequest();
        int n = mapper.UserInfoChange(email, realName, username);
        if(n < 1) {
            res.setStatus(1);
        } else {
            res.setStatus(0);
            res.setMsg("success");
        }
        return res;
    }

    public NoteRequest UserRemove(String username) {
        NoteRequest res = new NoteRequest();
        int n = mapper.UserRemove(username);
        if(n < 1) {
            res.setStatus(1);
        } else {
            res.setStatus(0);
            res.setMsg("success");
        }
        return res;
    }


}
